<?php

namespace App\Admin\Models\Financial;

use App\Admin\Models\AdminPaytypeList;
use App\Common\Services\Common;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class FinancialMoneylog extends Model
{
    protected $table = 'admin_financial_moneylog';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request,$type)
    {
        $query = self::with(['paytypeinfo','subjectinfo'])->where(['is_del' => 1,'type'=>$type])->orderBy('create_at', 'asc')->orderBy('id', 'asc');



        $date_start = $request->date_start;
        $date_end = $request->date_end;
        if ($date_start && $date_end) {
            $query->where('log_date', '>=',$date_start)->where('log_date', '<=',$date_end);
        }
        $log_date=$request->log_date;
        if($log_date!=''){
            $query->where(['log_date'=>$log_date]);
        }

        $receipt_no=$request->receipt_no;
        if($receipt_no!=''){
            $query->where([['receipt_no','like','%'.$receipt_no.'%']]);
        }

        $pay_type_id=intval($request->pay_type_id);
        if($pay_type_id>0){
            $query->where(['pay_type_id'=>$pay_type_id]);
        }

        $subject_id=intval($request->subject_id);
        if($subject_id>=-5  && $subject_id!=0){
            $query->where(['subject_id'=>$subject_id]);
        }




        $isgetall = intval($request->isgetall) ?? 0;

        if ($isgetall == 0) {
            $limit = $request->limit ?? 10;
            $list = $query->paginate($limit)->toArray();
            return Common::paginateDataToArray($list);
        } else {
            $list = $query->get()->toArray();
            return ['list' => $list];
        }
    }

    public function paytypeinfo(){
        return $this->hasOne(AdminPaytypeList::class,'id','pay_type_id');
    }

    public function subjectinfo(){
        return $this->hasOne(FinancialSubject::class,'id','subject_id');
    }
}
